<?php

class Language {

    // --- ATTRIBUTES ---

    /**
     * Selected language from current user
     * @var String[2]
     */
    private $languageID = 'en';

    /**
     * Default language from website
     * @var String[2]
     */
    private $defaultLanguageID = 'en';

    var $lang_module = array(); //

    // --- OPERATIONS ---

    /**
     * Constructor of current class
     * reads $languageID and $defaultLanguageID from settings
     */
    public function __construct() {
        // read $defaultLanguageID
        $this->readDefaultLanguageID();

        // read $languageID
        $_SESSION['languageID'] = $this->readLanguageID();

    }

    /**
     * reads default language from database and save to $defaultLanguageID
     */
    private function readDefaultLanguageID() {
        global $MySQL_DB;

        $res_lang = execute_query("SELECT language_ID FROM %PRE%settings",$MySQL_DB);
        if($lang = $res_lang->fetch_object()) {
            $this->defaultLanguageID = $lang->language_ID;
        }
        else {
            write_errorlog('Failed to read default language!',1,'class.language.php');
        }
    }

    /**
     * reads language from session or user database and save to $languageID
     */
    private function readLanguageID() {
        global $MySQL_DB;

        // read from session
        if(isset($_SESSION['languageID'])) {
            // LanguageID must be valid and in database
            if(isValidLanguageID($_SESSION['languageID']) == 2) {
                $this->languageID = $_SESSION['languageID'];
                return $this->languageID;
            }
        }

        // read from user settings
        if(isset($_SESSION['userID'])) {
            $res_lang = execute_query("SELECT language_ID FROM %PRE%user WHERE ID = ".$_SESSION['userID']."",$MySQL_DB);
            if($lang = $res_lang->fetch_object()) {
                $this->languageID = $lang->language_ID;
                return $this->languageID;
            }
        }

        // use default language
        $this->languageID = $this->defaultLanguageID;
        return $this->languageID;
    }

    /**
     * reads and returns translation from database with different languages
     *
     * @param  Integer $ID -> translations.ID
     * @param  String $type -> translations.type
     * @param  Integer $length -> 1 = small text (max. 255); 2 = long text
     * @param  String[2] $language -> fixe language
     * @return String
     */
    public function translation($ID, $type, $length, $language='') {
        global $MySQL_DB;

        // no ID passed?
        if($ID == 0) {
            return '';
        }

        // small or long text?
        if($length == 1)
            $select_field = 'translations.small_text';
        else
            $select_field = 'translations.long_text';

        // special language set?
        if(strlen($language) > 0) {
            $res_translation = execute_query("SELECT ".$select_field." 'text' FROM translations WHERE translations.ID = ".$ID." and translations.type = '".$type."' and translations.language_ID = '".$language."'",$MySQL_DB);
            if($translation = $res_translation->fetch_object())
                return $translation->text;
        }

        // read with users language
        $res_translation = execute_query("SELECT ".$select_field." 'text' FROM translations WHERE translations.ID = ".$ID." and translations.type = '".$type."' and translations.language_ID = '".$this->languageID."'",$MySQL_DB);
        if($translation = $res_translation->fetch_object())
            return $translation->text;

        // read with default language
        $res_translation = execute_query("SELECT ".$select_field." 'text' FROM translations WHERE translations.ID = ".$ID." and translations.type = '".$type."' and translations.language_ID = '".$this->defaultLanguageID."'",$MySQL_DB);
        if($translation = $res_translation->fetch_object())
            return $translation->text;

        // read with english language
        $res_translation = execute_query("SELECT ".$select_field." 'text' FROM translations WHERE translations.ID = ".$ID." and translations.type = '".$type."' and translations.language_ID = 'en'",$MySQL_DB);
        if($translation = $res_translation->fetch_object())
            return $translation->text; 

        // nothing found?
        write_errorlog('No translation for: ID: '.$ID.'; TYPE: '.$type,3,'class.language.php');
        return 'empty_string';
    }

    // --- GET/SET METHODS ---

    public function getLanguageID() {
        return $this->languageID;
    }

    public function getDefaultlanguageID() {
        return $this->defaultLanguageID;
    }


}

?>